import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:social_module/model/list_model.dart';

class ListProvider extends ChangeNotifier {
  final ListModel _listModel = ListModel();

  List<List<String>> _list = [];

  get list => _list;

  get length => _list.length;

  getList() {
    var res = _listModel.getAll();
    res.then((value) {
      _list.clear();
      for (var row in value) {
        _list.add(row);
      }
      notifyListeners();
    });
  }

  init() {
    getList();
  }

  void addToList(String user_id, String time, String comment) {
    var res = _listModel.insert(user_id, time, comment);
    res.then((value) {
      _list.clear();
      for (var row in value) {
        _list.add(row);
      }
      notifyListeners();
    });
  }

  void change() {
    var num1 = new Random().nextInt(_list.length);
    var num2 = new Random().nextInt(_list.length);
    while (num1 == num2) {
      num2 = new Random().nextInt(_list.length);
    }
    List<String> tmp = _list[num1];
    _list[num1] = _list[num2];
    _list[num2] = tmp;
    notifyListeners();
  }
}
